<template>
  <div :id="id" :style="style"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'BaseCharts',
  props: {
    id: {
      type: String
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    option: {
      type: Object
    }
  },
  data() {
    return {
      chart: null
    }
  },
  computed: {
    style() {
      return {
        width: this.width,
        height: this.height
      }
    }
  },
  watch: {
    option: {
      handler(val) {
        if (this.chart) {
          this.chart.setOption(val)
        } else {
          this.initChart()
        }
      },
      deep: true
    }
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      if (!this.option) {
        return
      }
      let dom = document.getElementById(this.id)
      this.chart = echarts.init(dom)
      this.chart.setOption(this.option)
      this.$emit('inited', this.chart)
    }
  }
}
</script>

<style scoped lang="scss">

</style>